package com.example.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.myapplication.Fragment.CityListFragment
import com.example.myapplication.Fragment.ProfileFragment
import com.example.myapplication.Fragment.TodoFragment
import com.example.myapplication.Fragment.WeatherFragment
import com.google.android.material.bottomnavigation.BottomNavigationView

class MainActivity : AppCompatActivity() {

    private lateinit var bottomNavigationView: BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bottomNavigationView = findViewById(R.id.bottom_navigation)
        bottomNavigationView.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.action_todo -> loadFragment(TodoFragment())
                R.id.action_profile -> loadFragment(ProfileFragment())
                R.id.action_city_list -> loadFragment(CityListFragment())
            }
            true
        }

        // Load the first fragment
        if (savedInstanceState == null) {
            loadFragment(TodoFragment())
        }
    }

    private fun loadFragment(fragment: Fragment) {
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commit()
    }

    fun showWeatherFragment(cityCode: String) {
        val weatherFragment = WeatherFragment().apply {
            arguments = Bundle().apply {
                putString("city_code", cityCode)
            }
        }
        loadFragment(weatherFragment)
    }
}